#include "EditorAudioPageViewModel.h"
#include "Audio/Audio.h"
#include "Audio/AudioSubsystem.h"

#include "Asset/EngineDefaultPackage.h"
#include "Asset/EditorDefaultPackage.h"
#include "Math/Math.h"

#include "Containers/Format.h"

void REditorAudioPageViewModel::BindModel(TSharedObjectPtr<REditorPageModel> InModel)
{
    Super::BindModel(InModel);
    Model = InModel;

    Icon= REditorDefaultPackage::Get()->GetIcon(U"SoundIcon");

    PlayIcon= REditorDefaultPackage::Get()->GetIcon(U"PlayerPlay");
    PauseIcon= REditorDefaultPackage::Get()->GetIcon(U"PlayerPause");

    VolumeFullIcon= REditorDefaultPackage::Get()->GetIcon(U"VolumeFull");
    VolumeMuteIcon= REditorDefaultPackage::Get()->GetIcon(U"VolumeMute");

    AudioPlayer= RAudioSubsystem::GetInstance()->CreateAudioPlayer(GetAudio());
}

TSharedObjectPtr<RTexture2D> REditorAudioPageViewModel::GetIcon() const
{
    return Icon;
}

CString REditorAudioPageViewModel::GetTitle() const
{
    return Model->Audio->GetNodeName();
}

TSharedObjectPtr<RAudio> REditorAudioPageViewModel::GetAudio() const
{
    return Model->Audio;
}

TSharedObjectPtr<RTexture2D> REditorAudioPageViewModel::GetPlayIcon() const
{
    if(AudioPlayer->IsPlaying())
    {
        return PauseIcon;
    }
    else
    {
        return PlayIcon;
    }
}

CString REditorAudioPageViewModel::GetTimeText() const
{
    int64_t Seconds=CMath::Ceil(AudioPlayer->GetCurrentTime());
    int64_t TotalSeconds=CMath::Ceil(AudioPlayer->GetAudio()->GetLength());

    int64_t Minutes=Seconds/60;
    Seconds=Seconds%60;
    CString SecondsStr=Seconds<10?U"0"+CString(Seconds):CString(Seconds);

    int64_t TotalMinutes=TotalSeconds/60;
    TotalSeconds=TotalSeconds%60;
    CString TotalSecondsStr=TotalSeconds<10?U"0"+CString(TotalSeconds):CString(TotalSeconds);

    return WH::Format(U"{}:{}/{}:{}",Minutes,SecondsStr,TotalMinutes,TotalSecondsStr);
}

void REditorAudioPageViewModel::TogglePlay()
{
    if(AudioPlayer->IsPlaying())
    {
        AudioPlayer->Pause();
    }
    else
    {
        if(AudioPlayer->IsOpen()==false)
        {
            AudioPlayer->Open();
        }
        AudioPlayer->Play();
    }
}

float REditorAudioPageViewModel::GetCurrentTimePercent() const
{
    return AudioPlayer->GetCurrentTimePercent();
}

void REditorAudioPageViewModel::SetCurrentTimePercent(float InPercent)
{
    AudioPlayer->SetCurrentTimePercent(InPercent);
}

TSharedObjectPtr<RTexture2D> REditorAudioPageViewModel::GetVolumeIcon() const
{
    if(AudioPlayer->Volume==0)
    {
        return VolumeMuteIcon;
    }
    else
    {
        return VolumeFullIcon;
    }
}

float REditorAudioPageViewModel::GetVolume() const
{
    return AudioPlayer->Volume;
}

void REditorAudioPageViewModel::SetVolume(float InVolume)
{
    AudioPlayer->Volume=InVolume;
}

void REditorAudioPageViewModel::ToggleMute()
{
    if(AudioPlayer->Volume==0)
    {
        AudioPlayer->Volume= VolumeBeforeMute;
    }
    else
    {
        VolumeBeforeMute=AudioPlayer->Volume;
        AudioPlayer->Volume=0;
    }
}
